<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col class="aaa" :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal" />
      </el-col>
      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px" style="display: flex;justify-content: space-between;">
          <div>
            <el-button style="margin-right: 6px" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
            <el-form-item label="拌合楼" prop="stationCode">
              <el-select style="width: 160px" v-model="queryParams.stationCode" clearable size="mini" placeholder="请选择拌合楼">
                <el-option v-for="item in cementListData" :key="item.id" :label="item.name" :value="item.code"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="用料项目">
              <el-input v-model="queryParams.projectName" placeholder="用料项目" clearable filterable></el-input>
            </el-form-item>
            <el-form-item label="配方编号">
              <el-input v-model="queryParams.matchCode" placeholder="配方编号" clearable filterable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
          </div>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
        
        <el-table v-loading="loading" :data="cementDataList" :header-cell-style="{ 'text-align': 'center' }" fit border size="mini" highlight-current-row stripe>
          <el-table-column prop="projectName" label="用料项目" align="center" width="75px" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.projectName || "-" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="stationName" label="拌合楼" align="center" min-width="75px" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.stationName || "-" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="pavingLevelLabel" label="摊铺层级" align="center" min-width="75px" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.pavingLevelLabel || "-" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="matchCode" label="配方编号" align="center" min-width="75px" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.matchCode || "-" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="gravel1" label="集料1" align="center" min-width="110px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="gravel2" label="集料2" align="center" min-width="80px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="gravel3" label="集料3" align="center" min-width="80px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="gravel4" label="集料4" align="center" min-width="75px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="gravel5" label="集料5" align="center" min-width="75px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="gravel6" label="集料6" align="center" min-width="75px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="powder1" label="水泥1" align="center" min-width="60px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="powder2" label="水泥2" align="center" min-width="60px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope | showGravelAndPowder }}</template>
          </el-table-column>
          <el-table-column prop="water" label="水" align="center" min-width="60px" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.water | showNumber }}</template>
          </el-table-column>
          <el-table-column prop="updateName" label="修改人" align="center" min-width="75px" show-overflow-tooltip></el-table-column>
          <el-table-column prop="updateDate" label="修改时间" align="center" width="135px"></el-table-column>
          <el-table-column label="操作" align="center" width="100px" fixed="right">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleEdit(scope.row)">编辑</el-link>
              <el-divider direction="vertical"></el-divider>
              <el-link type="primary" @click="handleDelete(scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="editForm" :model="form" :rules="rules" label-width="100px" class="myForm" size="mini" v-loading="loading">
        <el-row :gutter="0">
          <el-col :span="12">
            <el-form-item label="拌合楼" prop="stationCode">
              <el-select v-model="form.stationCode" clearable :disabled="isEdit" placeholder="请选择拌合楼">
                <el-option v-for="item in cementListData" :key="item.id" :label="item.name" :value="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属项目" prop="projectId">
              <el-select :disabled="isEdit" v-model="form.projectId" placeholder="所属项目" clearable :popper-append-to-body="false">
                <el-option v-for="item of materielProjectSelectList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="配方编号" prop="matchCode">
              <el-input placeholder="配方编号" v-model="form.matchCode" :disabled="isEdit" maxlength="20" style="width: 91.5%;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摊铺层级" prop="pavingLevel">
              <el-select v-model="form.pavingLevel" placeholder="摊铺层级" clearable :popper-append-to-body="false">
                <el-option v-for="item of cementPavingLevelList" :key="item.code" :label="item.shortname" :value="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集料1" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel1Name">
                  <el-select v-model="form.gravel1Name" placeholder="集料1名称" clearable :popper-append-to-body="false">
                    <el-option v-for="(item, index) of waterMixrueList" :key="'gravel1' + index" :value="item.code" :label="item.description"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2"> </el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel1">
                  <el-input-number placeholder="集料1占比" v-model="form.gravel1" :controls="false" :max="100" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集料2" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel2Name">
                  <el-select v-model="form.gravel2Name" placeholder="集料2名称" clearable :popper-append-to-body="false">
                    <el-option v-for="(item, index) of waterMixrueList" :key="'gravel2' + index" :value="item.code" :label="item.description"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">&nbsp;</el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel2">
                  <el-input-number placeholder="集料2占比" :max="100" v-model="form.gravel2" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集料3" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel3Name">
                  <el-select v-model="form.gravel3Name" placeholder="集料3名称" clearable :popper-append-to-body="false">
                    <el-option v-for="(item, index) of waterMixrueList" :key="'gravel3' + index" :value="item.code" :label="item.description"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">&nbsp;</el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel3">
                  <el-input-number placeholder="集料3占比" v-model="form.gravel3" :max="100" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集料4" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel4Name">
                  <el-select v-model="form.gravel4Name" placeholder="集料4名称" clearable :popper-append-to-body="false">
                    <el-option v-for="(item, index) of waterMixrueList" :key="'gravel4' + index" :value="item.code" :label="item.description"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2"></el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel4">
                  <el-input-number placeholder="集料4占比" :max="100" v-model="form.gravel4" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集料5" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel5Name">
                  <el-select v-model="form.gravel5Name" placeholder="集料5名称" clearable :popper-append-to-body="false">
                    <el-option v-for="(item, index) of waterMixrueList" :key="'gravel5' + index" :value="item.code" :label="item.description"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">&nbsp;</el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel5">
                  <el-input-number placeholder="集料5占比" :max="100" v-model="form.gravel5" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集料6" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel6Name">
                  <el-select v-model="form.gravel6Name" placeholder="集料6名称" clearable :popper-append-to-body="false">
                    <el-option v-for="(item, index) of waterMixrueList" :key="'gravel6' + index" :value="item.code" :label="item.description"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">&nbsp;</el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="gravel6">
                  <el-input-number placeholder="集料6占比" :max="100" v-model="form.gravel6" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="水泥1" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="powder1Name">
                  <el-input placeholder="水泥1名称" maxlength="20" v-model="form.powder1Name" style="width: 85%;"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="2">&nbsp;</el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="powder1">
                  <el-input-number placeholder="水泥1占比" :max="100" v-model="form.powder1" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="水泥2" class="form-item-out">
              <el-col :span="10">
                <el-form-item label-width="0px" prop="powder2Name">
                  <el-input placeholder="水泥2名称" maxlength="20" v-model="form.powder2Name" style="width: 85%;"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="2">&nbsp;</el-col>
              <el-col :span="10">
                <el-form-item label-width="0px" prop="powder2">
                  <el-input-number placeholder="水泥2占比" :max="100" v-model="form.powder2" :controls="false" :min="0" :precision="2"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="水" prop="water">
              <el-col :span="22">
                <el-input-number placeholder="水" :max="100" v-model="form.water" :controls="false" :min="0" :precision="2"></el-input-number>
              </el-col>
              <el-col :span="2" class="colSplit">%</el-col>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-descriptions direction="vertical" class="descriptionsBox" :column="4" border>
              <el-descriptions-item label="序号">
                <div class="descriptionsDiv" style="font-size: 16px;padding: 3px 0;">1</div>
                <div class="descriptionsDiv" style="font-size: 16px;padding: 3px 0;">2</div>
                <div style="font-size: 16px;padding: 3px 0;">3</div>
              </el-descriptions-item>
              <el-descriptions-item label="名称">
                <div class="descriptionsDiv" style="font-size: 16px;padding: 3px 0;">水胶比</div>
                <div class="descriptionsDiv" style="font-size: 16px;padding: 3px 0;">水含量</div>
                <div style="font-size: 16px;padding: 3px 0;">水泥掺量外掺</div>
              </el-descriptions-item>
              <el-descriptions-item label="上限">
                <div><el-input placeholder="水胶比上限" v-model="form.sjbMaxVal" style="font-size: 16px;"></el-input></div>
                <div><el-input placeholder="水含量上限" v-model="form.waterMaxVal" style="font-size: 16px;"></el-input></div>
                <div><el-input placeholder="水泥掺量外掺上限" v-model="form.snMaxVal" style="font-size: 16px;"></el-input></div>
              </el-descriptions-item>
              <el-descriptions-item label="下限">
                <div><el-input placeholder="水胶比下限" v-model="form.sjbMinVal" style="font-size: 16px;"></el-input></div>
                <div><el-input placeholder="水含量下限" v-model="form.waterMinVal" style="font-size: 16px;"></el-input></div>
                <div><el-input placeholder="水泥掺量外掺下限" v-model="form.snMinVal" style="font-size: 16px;"></el-input></div>
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" :loading="btnLoading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import workSiteLeft from "@/views/components/workSiteLeft";
import { cementList } from "@/api/waterStability/producedData/monitor";
import { cementMatchList, cementMatchInfo, materielProjectSelect, getcodelistall, addCementMatch, cementMatchEdit, cementMatchRemove } from "@/api/waterStability/designMatching";
// import deatetime from "@/components/Datetime";

export default {
  name: "designMatching", //设计生产配比,
  components: {
    // deatetime,
    workSiteLeft
  },
  data() {
    return {
      show: true,
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      btnLoading: false,
      isEdit: false,
      materielProjectSelectList: [], //所属项目
      cementPavingLevelList: [], //摊铺层级
      waterMixrueList: [], //集科1——6
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      cementListData: [], //拌合楼列表
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水稳拌合站数据表格数据
      cementDataList: [],
      // 查询参数
      queryParams: {
        stationCode: null,
        projectName: "",
        matchCode: "",
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "请选择所属项目", trigger: "change" },
        ],
        stationCode: [
          { required: true, message: "请选择拌合楼", trigger: "change" },
        ],
        matchCode: [
          { required: true, message: "请输入配方编号", trigger: "blur" },
        ],
      },
    };
  },
  filters: {
    /*** 显示集料或粉料信息 */
    showGravelAndPowder(val) {
      let property = val.column.property;
      let data = val.row;
      let value = data[property];
      if (null == value || undefined == value) {
        return "/";
      }
      let name = data[property + "Name"];
      if (null != name && undefined != name) {
        name += ":";
      } else {
        name = "";
      }
      return name + value;
    },
    /*** 显示数字 */
    showNumber(val) {
      if (null == val || undefined == val) {
        return "/";
      }
      return val;
    },
  },
  created() {
    this.getCementList();
    this.getList();
  },
  methods: {
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      this.queryParams.id = val.id;
      this.handleQuery();
    },
    // 查询拌合楼下拉列表
    getCementList() {
      cementList().then((res) => {
        this.cementListData = res.data || [];
      });
    },
    /** 查询水稳拌合站数据列表 */
    getList() {
      this.loading = true;
      cementMatchList(this.queryParams).then((response) => {
        this.cementDataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /*** 获取摊铺层级 */
    getCementGravelList() {
      getcodelistall({
        codeid: "CEMENT_PAVING_LEVEL",
      }).then((res) => {
        this.cementPavingLevelList = res.data || [];
      });
    },
    /*** 获取集科1——6 */
    getCementGravelList2() {
      getcodelistall({
        codeid: "WATER_MIXTRUE",
      }).then((res) => {
        this.waterMixrueList = res.data || [];
      });
    },
    /*** 所属项目 */
    getMaterielProjectSelect() {
      materielProjectSelect().then((res) => {
        this.materielProjectSelectList = res.data || [];
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams = this.getSearchParams();
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getSearchParams() {
      const params = {};
      for (const key in this.queryParams) {
        if (key === "time") {
          const time = this.queryParams[key];
          if (time && time.length === 2) {
            params.startTime = time[0];
            params.endTime = time[1];
          }
        } else {
          params[key] = this.queryParams[key];
        }
      }
      return params;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.stationCode = null;
      this.queryParams.projectName = "";
      this.queryParams.matchCode = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增
    handleAdd() {
      this.form = {};
      this.isEdit = false;
      this.getCementList();
      this.getCementGravelList();
      this.getCementGravelList2();
      this.getMaterielProjectSelect();
      this.open = true;
      this.title = "添加设计生产配比";
    },
    // 编辑
    handleEdit(row) {
      this.form = row || {};
      this.isEdit = true;
      this.getCementList();
      this.getCementGravelList();
      this.getCementGravelList2();
      this.getMaterielProjectSelect();
      this.open = true;
      this.title = "修改设计生产配比";
    },
    handleDelete(row) {
      this.$confirm("此操作将删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      .then(() => {
        cementMatchRemove(row.id).then((res) => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      })
      .catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.btnLoading = true;
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          // this.form.stationCode = "111";
          console.log(this.form);
          if (this.form.id) {
            cementMatchEdit(this.form).then((res) => {
              this.$message.success("修改设计生产配比成功！");
              this.getList();
              this.btnLoading = false;
              this.open = false;
            });
          } else {
            addCementMatch(this.form).then((res) => {
              this.$message.success("新增设计生产配比成功！");
              this.getList();
              this.btnLoading = false;
              this.open = false;
            });
          }
        } else {
          this.btnLoading = false;
        }
      });
    },
    cancel() {
      this.getList();
      this.open = false;
    },
  },
};
</script>
<!-- <style scoped lang="scss">
.app-designMatching {
  padding-top: 16px;
  user-select: none;
}
</style> -->

<style scoped>
.myForm>>>.el-input-number {
  width: 100%;
}

.myForm>>>.el-input__inner {
  text-align: left;
}

.myForm>>>.el-input-number {
  width: 100%;
}

.myForm>>>.el-input__inner {
  text-align: left;
}

.form-item-out {
  margin-bottom: 0px;
}

.colSplit {
  text-align: center;
  font-size: 16px;
}

.el-col {
  border: 1px solid transparent;
}

.descriptionsBox>>>div {
  text-align: center;
  font-size: 20px;
  font-weight: 400;
}

.descriptionsBox>>>.descriptionsDiv {
  /* border-bottom: 2px solid #e6ebf5; */
  border-bottom: 1px solid #e6ebf5;
}
</style>

